package copy;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;



public class Copy extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton[] bt = new JButton[2];
	private JButton copy;
	private JTextField[] tf=new JTextField[2];
	private JProgressBar pb;
	private File srcFile;
	public Copy() {
		setSize(500, 500);
		setLocation(700,200);//显示位置
		setVisible(true);// 显示窗体
		setTitle("复制窗口");
		setDefaultCloseOperation(EXIT_ON_CLOSE);		
		setLayout(null);
		String[] str={"源文件","目标文件"};
		int x=50,y=50;
		for (int i = 0; i < str.length; i++) {
			JLabel lable=new JLabel(str[i]);
			lable.setBounds(x,y,70,25);
			add(lable);
			tf[i]=new JTextField();
			tf[i].setBounds(x+lable.getWidth(), y, 200, 25);
			add(tf[i]);
			bt[i]=new JButton("...");
			bt[i].setBounds(x+lable.getWidth()+tf[i].getWidth(), y, 50, 25);
			bt[i].addActionListener(this);
			add(bt[i]);
			y+=50;
		}
		pb=new JProgressBar(0,100);
		pb.setBounds(80,220,300,25);
		pb.setStringPainted(true);
		//pb.setValue(30);
		add(pb);
		
		copy=new JButton("复制");
		copy.setBounds(150, 300, 200, 25);
		copy.addActionListener(this);
		add(copy);
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			// 写个内部内来实现输入窗口
			@Override
			public void run() {
				new Copy();
			}
		});
	}
	@Override
	public void actionPerformed(ActionEvent e) {		
		Object o=e.getSource();
		if(o==bt[0]){
			JFileChooser fc = new JFileChooser();
			int f=fc.showOpenDialog(this);
			if(f==JFileChooser.APPROVE_OPTION){
				srcFile=fc.getSelectedFile();
				tf[0].setText(srcFile.getPath());
			}
		}else if(o==bt[1]){
			JFileChooser fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int f=fc.showSaveDialog(this); 
			if(f==JFileChooser.APPROVE_OPTION){
				tf[1].setText(fc.getSelectedFile().getPath());
			}
		}else if(o==copy){
			File desFile;
			String path=tf[1].getText()+"\\"+srcFile.getName();
			desFile=new File(path);
			new Thread(new Runnable() {
				public void run() {
					try {
						BufferedInputStream in=new BufferedInputStream(new FileInputStream(srcFile));
						BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(desFile));
						long size=srcFile.length();
						int a,percent,c=0;
						while((a=in.read())!=-1){
							out.write(a);
							c++;
							percent = (int) ((1.0*c/size)*100);
							pb.setValue(percent);	
						}
						in.close();out.flush();out.close();
					}catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}					
				}
			}).start();
		}
	}
}
